package com.wyw.design.model.learning.structure.flyweight;

import java.util.HashMap;

/**
 * @Title       享元工厂
 * @Description
 * @Author Mr Wu yewen.wu.china@gmail.com
 * @Date 2023/3/30 18:19
 * Update History:
 * Author        Time            Content
 */
public class FlyweightFactory {

    // 池容器
    private static HashMap<String, Flyweight> pool = new HashMap<>();

    // 获取池中对象
    public static Flyweight getFlyweight(String extrinsic) {
        // 需要返回的对象
        Flyweight flyweight = null;
        if (!pool.containsKey(extrinsic)) {
            // 根据外部状态创建享元对象
            flyweight = new ConcreteFlyweight(extrinsic);
            // 再放入池中
            pool.put(extrinsic, flyweight);
        } else {
            // 池中有对象，则直接返回
            flyweight = pool.get(extrinsic);
        }
        return flyweight;
    }
}
